package com.kinpoway.web.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.kinpoway.domin.User;
import com.kinpoway.querycondition.UserQueryCondition;
import com.kinpoway.repository.UserRepository;
import com.kinpoway.services.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@Api(value = "用户信息",description = "用户信息")
@RestController
@RequestMapping("/user")

public class UserController{

    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;

    // 单表分页列表

    //TODO Pageable分页与JsonView视图同时怎么用???
    @ApiOperation(value="获取用户分页列表", notes="传入分页参数获取用户信息形成分页列表")
    @GetMapping
//    @JsonView(User.UserSimpleView.class)
    public Page<User> getUserListPage(@PageableDefault()Pageable pageable){
        Page<User> users = userRepository.findAll(pageable);
        return users;
    }

    // 单表全部信息列表
    @ApiOperation(value="获取全部用户列表", notes="无参数直接获取所有用户信息形成列表")
    @GetMapping("/all")
    @JsonView(User.UserDetailView.class)
    public List<User> getUserAll(){
        List<User> users = userRepository.findAll();
        return users;
    }

    // 通过id获取user
    @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @GetMapping("/{id:\\d+}")  //通过正则表达式来校验参数
    @JsonView(User.UserSimpleView.class)
    public User getUserById(@PathVariable Integer id){
        User user = userRepository.findOne(id);
        System.out.println(user);   // 这里打印的时间是数据库中的时间
        return user;
    }


    // 添加
    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @PostMapping
    @JsonView(User.UserInfoView.class)
    public User createUser(@Valid @RequestBody User user, BindingResult bindingResult){
        // 控制台输出错误信息
        if(bindingResult.hasErrors()){
            // 一般来说在这里打印错误日志
            bindingResult.getAllErrors().stream().forEach(objectError -> System.out.println(objectError.getDefaultMessage()));
        }
        return userRepository.save(user);
    }

    // 更新
    @ApiOperation(value="更新信息", notes="根据url的id来指定更新用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "user", value = "用户实体user", required = true, dataType = "User")
    })
    @PutMapping
    @JsonView(User.UserInfoView.class)
    public User updateUser(@Valid @RequestBody User user, BindingResult bindingResult){
        // 控制台输出错误信息
        if(bindingResult.hasErrors()){
            // 一般来说在这里打印错误日志
            bindingResult.getAllErrors().stream().forEach(objectError -> System.out.println(objectError.getDefaultMessage()));
        }
        return userRepository.save(user);
    }

    // 删除
    @ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @DeleteMapping("/{id:\\d+}")
    public void deleteUser(@PathVariable Integer id){
        userRepository.delete(id);
    }


    //TODO  根据一个对象来查询过滤结果集,类似列表搜索.把所有的搜索条件封装成一个对象,与前端对应.以满足列表过滤搜索.
    @ApiIgnore//使用该注解忽略这个API
    @PostMapping("/{userQueryContition}")
    public List<User> filterListGetUsers(@RequestParam("userQueryContition") UserQueryCondition userQueryContition){
        List<User> users  = userService.filterListGetUsers(userQueryContition);
        System.out.println(users);   // 这里打印的时间是数据库中的时间
        return users;
    }


}
